import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';

import 'SampleListItem.dart';

/**
 *Created by Jcy
 *on 2021/9/27
 */
class FreshListView extends StatefulWidget {
  final itemBuilder;
  final onRefresh;
  final onLoad;
  final length;

// 方向
  final direction;

  const FreshListView({
    Key? key,
    required this.itemBuilder,
    required this.length,
    this.onRefresh,
    this.onLoad,
    this.direction = Axis.vertical,
  }) : super(key: key);

  @override
  _FreshListViewState createState() => _FreshListViewState();
}

class _FreshListViewState extends State<FreshListView> {


  @override
  Widget build(BuildContext context) {
    return EasyRefresh(
      header: ClassicalHeader(
        enableInfiniteRefresh: false,
        bgColor: Colors.white,
        infoColor: Colors.black,
        // enableHapticFeedback: vibration, //// 震动
        refreshText: '拉动刷新',
        refreshReadyText: '释放刷新',
        refreshingText: '正在刷新...',
        refreshedText: '刷新完成',
        refreshFailedText: '刷新失败',
        noMoreText: '没有更多数据',
        infoText: '更新于 %T',
      ),
      footer: ClassicalFooter(
        bgColor: Colors.white,
        infoColor: Colors.black,
        loadText: '拉动加载',
        loadReadyText: '释放加载',
        loadingText: '正在加载...',
        loadedText: '加载完成',
        loadFailedText: '加载失败',
        noMoreText: '没有更多数据',
        infoText: '更新于 %T',
      ),
      firstRefresh: true,
      firstRefreshWidget: Container(
        width: double.infinity,
        height: double.infinity,
        child: Center(
            child: SizedBox(
          height: 400.w,
          width: 600.w,
          child: Card(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                Container(
                  width: 100.w,
                  height: 100.w,
                  child: SpinKitFadingCube(
                    color: Theme.of(context).primaryColor,
                    size: 50.w,
                  ),
                ),
                Container(
                  child: Text('正在加载...'),
                )
              ],
            ),
          ),
        )),
      ),
      child: ListView.builder(
          scrollDirection: this.widget.direction,
          itemCount: this.widget.length,
          itemBuilder: (context, index) {
            return this.widget.itemBuilder(index);
          }),
      onRefresh: this.widget.onRefresh,
      onLoad: this.widget.onLoad,
    );
  }
}
